/*******************************************************************************
 * Copyright (c) 2009 Dop Sun.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Dop Sun - initial API and implementation
 ******************************************************************************/
package com.softao.ming.gde.internal.ui.launcher;

import java.util.TreeSet;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.pde.core.plugin.TargetPlatform;
import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;

import com.softao.ming.gde.internal.core.util.PdeCoreUtil;

/**
 * 
 */
public class ProgramBlock extends AbstractGroupBlock {
	class Listener extends SelectionAdapter {
		public void widgetSelected(SelectionEvent e) {
			Object source = e.getSource();
			if (source == fProductButton) {
				boolean enabled = fProductButton.getSelection();
				fProductCombo.setEnabled(enabled);
			}
			getTab().updateLaunchConfigurationDialog();
		}
	}
	
	private Button fProductButton;
	private Combo fProductCombo;
	private Listener fListener = new Listener();

	/**
	 * @param tab
	 */
	public ProgramBlock(GwtLaunchTab tab) {
		super(tab);
	}

	@Override
	protected void createContent(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		parent.setLayout(layout);
		parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		createProductSection(parent);
	}

	@Override
	protected String getText() {
		return "Program to Run";
	}

	private void createProductSection(Composite parent) {
		fProductButton = new Button(parent, SWT.RADIO);
		fProductButton.setText("Run a &product:");
		fProductButton.addSelectionListener(fListener);

		fProductCombo = new Combo(parent, SWT.READ_ONLY | SWT.DROP_DOWN);
		fProductCombo.setItems(getProductNames());
		fProductCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		fProductCombo.addSelectionListener(fListener);
	}

	public void initializeFrom(ILaunchConfiguration config) throws CoreException {
		initializeProductSection(config);

		boolean useProduct = config.getAttribute(IPDELauncherConstants.USE_PRODUCT, false) && fProductCombo.getItemCount() > 0;
		fProductButton.setSelection(useProduct);
		fProductButton.setEnabled(fProductCombo.getItemCount() > 0);
		fProductCombo.setEnabled(useProduct);
	}

	private void initializeProductSection(ILaunchConfiguration config) throws CoreException {
		if (fProductCombo.getItemCount() > 0) {
			String productName = config.getAttribute(IPDELauncherConstants.PRODUCT, (String) null);
			int index = productName == null ? -1 : fProductCombo.indexOf(productName);
			if (index == -1)
				index = 0;
			fProductCombo.setText(fProductCombo.getItem(index));
		}
	}

	public void performApply(ILaunchConfigurationWorkingCopy config) {
		saveProductSection(config);
	}

	private void saveProductSection(ILaunchConfigurationWorkingCopy config) {
		config.setAttribute(IPDELauncherConstants.USE_PRODUCT, fProductButton.getSelection());
		config.setAttribute(IPDELauncherConstants.PRODUCT, fProductCombo.getText());
	}

	public void setDefaults(ILaunchConfigurationWorkingCopy config) {
		String product = TargetPlatform.getDefaultProduct();
		if (product != null) {
			config.setAttribute(IPDELauncherConstants.USE_PRODUCT, true);
			config.setAttribute(IPDELauncherConstants.PRODUCT, product);
		}
	}

	private static TreeSet<String> getProductNameSet() {
		TreeSet<String> result = new TreeSet<String>();
		IExtension[] extensions = PdeCoreUtil.findExtensions("com.softao.ming.gwt.core.runtime.products", true); //$NON-NLS-1$
		for (int i = 0; i < extensions.length; i++) {
			IConfigurationElement[] elements = extensions[i].getConfigurationElements();
			for (IConfigurationElement element : elements) {
				if ("product".equals(element.getName())) {
					String id = element.getAttribute("id");
					if (id != null && id.trim().length() > 0) {
						result.add(id);
					}
				}
			}
		}
		return result;
	}

	private static String[] getProductNames() {
		TreeSet<String> result = getProductNameSet();
		return result.toArray(new String[result.size()]);
	}
}
